using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class SequenceField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new SequenceField();
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            if (cellType == DataControlCellType.DataCell)
            {
                LiteralControl lit = new LiteralControl();
                lit.ID = "litSequence";
                cell.Controls.Add(lit);

                Button btnUp = new Button();
                btnUp.ID = "btnUp";
                btnUp.Text = "Up";

                cell.Controls.Add(btnUp);

                btnUp.Click += new EventHandler(btnUp_Click);

                if (rowState == DataControlRowState.Edit || rowState == DataControlRowState.Insert)
                {
                    btnUp.Enabled = false;
                }
                else
                {
                    btnUp.Enabled = true;
                }
            }



            cell.DataBinding += new EventHandler(cell_DataBinding);
        }

        void btnUp_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow row = btn.NamingContainer as GridViewRow;
            GridView gv = row.NamingContainer as GridView;

            //get old values populated
            indexClicked = row.RowIndex;
            for (int i = 0; i < gv.Rows.Count; i++)
            {
                gv.UpdateRow(i, false);
            }
        }

        void cell_DataBinding(object sender, EventArgs e)
        {
            DataControlFieldCell cell = sender as DataControlFieldCell;
            GridViewRow row = cell.NamingContainer as GridViewRow;

            if (row.RowType == DataControlRowType.DataRow)
            {
                Button btnUp = cell.FindControl("btnUp") as Button;
                if (btnUp != null)
                {
                    if (row.RowIndex == 0)
                    {
                        btnUp.Visible = false;
                    }
                }
            }
        }

        private int indexClicked = -1;

        protected override void CopyProperties(DataControlField newField)
        {
            base.CopyProperties(newField);
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if(!string.IsNullOrEmpty(this.DataField))
            {
                GridViewRow row = cell.NamingContainer as GridViewRow;
                int val = -1;
                if (row.RowIndex == indexClicked)
                {
                    val = row.RowIndex - 1;
                }
                else if (row.RowIndex == indexClicked - 1)
                {
                    val = row.RowIndex + 1;
                }
                else
                {
                    val = row.RowIndex;
                }

                if (dictionary.Contains(DataField))
                {
                    dictionary[DataField] = val;
                }
                else
                {
                    dictionary.Add(DataField, val);
                }

            }
        }

        


        private string dataField;
        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }
	
    }
}
